【Nginx 日志配置】【访问日志log |
您所在的位置:网站首页 › access 日志格式 › 【Nginx 日志配置】【访问日志log |
文章目录
Nginx 日志配置`log_format` 指令中常用的一些变量:访问日志 案例可以配置段:`http`, `stream`, `server`, `location`作用域。error_log 指令 错误访问日志 级别rewrite_log 指令 用户跳转日志 一般不用配置流量控制相关功能配置日志记录
Nginx 日志配置总结Nginx的日志轮转**
Nginx 日志配置
1、Nginx 日志介绍 Nginx 有一个非常灵活的日志记录模式,每个级别的配置可以有各自独立的访问日志, 所需日志模块 ngx_http_log_module 的支持,日志格式通过 log_format 命令来定义,日志对于统计和排错是非常有利的,下面总结了 nginx 日志相关的配置 包括 access_log、rewrite_log、error_log。 设置访问日志 access_log path 样式; 2、作用域 可以应用access_log指令的作用域分别有http,server,location。也就是说,在这几个作用域外使用该指令,Nginx会报错。 3、log_format 指令 Nginx 预定义了名为 combined 日志格式,如果没有明确指定日志格式默认使用该格式: log_format combined '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent"';面试时:注意日志里面的ip地址一定要在第一列。 访问日志 自定义 server下面写 我这里是使用的yum 安装的nginx 支持在子配置文件下 编写server文件 使用log_format指令定义了一个main的格式,并在access_log指令中引用了它。假如客户端有发起请求:ip,我们看一下我截取的一个请求的日志记录: 针对 server块 步骤:/etc/nginx/conf.d/ 进入这个目录下 步骤:vim ceshi.conf 编写一个 子配置文件以conf 结尾 log_format ming '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; server { listen 80; server_name localhost; access_log /var/log/nginx/ming.access.log ming; location / { root /usr/share/nginx/html; index index.html index.htm; } }
针对 location块 编写配置文件 log_format qing '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; access_log /var/log/nginx/qing.access.log qing; } }
错误日志在Nginx中是通过error_log指令实现的。该指令记录服务器和请求处理过程中的错误信息。 语法 error_log file [level]; Default: error_log logs/error.log warn;file 参数指定日志的写入位置。 level 参数指定日志的级别。level可以是debug, info, notice, warn, error, crit, alert,emerg中的任意值。可以看到其取值范围是按紧急程度从低到高排列的。只有日志的错误级别等于或高于level指定的值才会写入错误日志中。 基本用法 error_log /var/logs/nginx/nginx-error.log; 配置段:http, mail, stream, server, location作用域。 例子中指定了错误日志的路径为:/var/logs/nginx/nginx-error.log,日志级别使用默认的 error。 跟上面 操作一样 ,我这里就不一一举例示范拉 rewrite_log 指令 用户跳转日志 一般不用由ngx_http_rewrite_module模块提供的。用来记录重写日志的。对于调试重写规则建议开启,启用时将在error log中记录重写日志。 基本语法: rewrite_log on | off; 默认值: rewrite_log off;配置段: http, server, location, if作用域。 配置流量控制相关功能 配置日志记录默认情况下,Nginx会在日志中记录由于流量限制而延迟或丢弃的请求,如下所示:
以上已经涵盖了Nginx提供的“流量限制”的很多功能,包括为HTTP请求的不同location设置请求速率,给“流量限制”配置burst参数。 Nginx 日志配置总结Nginx中通过access_log和error_log指令配置访问日志和错误日志,通过log_format我们可以自定义日志格式。 详细的日志配置信息可以参考Nginx官方文档 Nginx的日志轮转** [root@192 ~]# rpm -ql nginx |grep log /etc/logrotate.d/nginx /var/log/nginx [root@192 ~]# vim /etc/logrotate.d/nginx /var/log/nginx/*.log { #指定需要轮转处理的日志文件 daily #日志文件轮转周期,可用值为: daily/weekly/yearly missingok # 忽略错误信息 rotate 7 # 轮转次数,即最多存储7个归档日志,会删除最久的归档日志 minsize 5M #限制条件,大于5M的日志文件才进行分割,否则不操作 dateext # 以当前日期作为命名格式 compress # 轮循结束后,已归档日志使用gzip进行压缩 delaycompress # 与compress共用,最近的一次归档不要压缩 notifempty # 日志文件为空,轮循不会继续执行 create 640 nginx nginx #新日志文件的权限 sharedscripts #有多个日志需要轮询时,只执行一次脚本 postrotate # 将日志文件转储后执行的命令。以endscript结尾,命令需要单独成行 if [ -f /var/run/nginx.pid ]; then #判断nginx的PID。# 默认logrotate会以root身份运行 kill -USR1 cat /var/run/nginx.pid fi endscript } 执行命令: [root@192 nginx]# /usr/sbin/logrotate -f /etc/logrotate.conf 创建计划任务: [root@192 nginx]# crontab -e 59 23 * * * /usr/sbin/logrotate -f /etc/logrotate.conf |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |